package com.cysion.study.widget;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

/**
 * Created by chu on 2019-11-10.
 */
public class LocationHelper {
    private static LocationHelper helper;
    private LocationClient mLocClient;
    private BDLocation mBdLocation;
    private Context mContext;
    private onGetLoc mListener;

    public static LocationHelper getInstance(Context context) {
        if (helper == null) {
            helper = new LocationHelper(context);
        }
        return helper;
    }

    public LocationHelper(Context context) {
        initLocation(context);
        mContext = context;
    }

    private void initLocation(Context context) {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll"); // 设置坐标类型  可选，默认gcj02，设置返回的定位结果坐标系
        option.setScanSpan(1000 * 10);// 可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setNeedDeviceDirect(true);//是否需要手机方向
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当GPS有效时按照1S/1次频率输出GPS结果
//        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
//        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        //option.setIgnoreKillProcess(false);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        //option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤GPS仿真结果，默认需要


        BDAbstractLocationListener mListeners=new BDAbstractLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {
                if (mListener != null) {
                    Log.e("百度定位", "定位成功"+bdLocation.getCity());
                    mListener.handleLocData(bdLocation);
                }
                mBdLocation = bdLocation;
                stopClient();
                //定位成功后停止定位
            }
        };

        BDLocationListener myListener = bdLocation -> {
            if (mListener != null) {
                mListener.handleLocData(bdLocation);
            }
            mBdLocation = bdLocation;
            stopClient();
            //定位成功后停止定位
        };
        mLocClient = new LocationClient(context);
        //可选，设置是否需要地址信息，默认不需要
        mLocClient.setLocOption(option);
//        mLocClient.registerLocationListener(myListener);
        mLocClient.registerLocationListener(mListeners);
    }

    public interface onGetLoc {
        void handleLocData(BDLocation bdLocation);
    }

    public BDLocation getBdLocation() {
        return mBdLocation;
    }

    public void startClient() {
        int permissionCheck = ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION);
        if (permissionCheck == PackageManager.PERMISSION_GRANTED && mBdLocation == null) {
            mLocClient.start();
        } else {
            Log.e("百度定位", "定位失败+定位未开启");
//            ToastUtilKt.ShowToast("目前手机定位未开启");
//            ToastUtil.show(mContext, "目前手机定位未开启", Toast.LENGTH_LONG);
        }

    }

    public void setListener(onGetLoc mListener) {
        this.mListener = mListener;
    }

    public void stopClient() {
        if (null != mLocClient) {
            mLocClient.stop();
        }
    }

}
